package com.zhongxi.guoyuan.view.personalCenter;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.personCenter.ClearEvaluateAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.clearweed.ClearEvaluateResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.CommonTool;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ClearWeedEvaluateActivity extends BaseActivity implements AbsListView.OnScrollListener {


    @Bind(R.id.ivBack)
    ImageView ivBack;
    @Bind(R.id.tvTitle)
    TextView tvTitle;
    @Bind(R.id.mylistview)
    ListView mylistview;
    @Bind(R.id.no_evaluate)
    TextView noEvaluate;

    ProgressBar defaultFooterProgressbar;
    TextView defaultFooterTitle;
    private ClearEvaluateResult evaluateResult;

    private List<ClearEvaluateResult.Comments> commentsList = new ArrayList<>();
    private ClearEvaluateAdapter evaluateAdapter;
    private JSONObject user;
    private String id;
    private boolean islast = false;
    private boolean nomore = false;


    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_main2);
        ButterKnife.bind(this);
        initTitle();
        tvTitle.setText("豆豆兑换商品评价");

        initFooter();

    }

    private void initFooter() {
        View view = LayoutInflater.from(this).inflate(R.layout.default_footer, null);
        defaultFooterProgressbar = (ProgressBar) view.findViewById(R.id.default_footer_progressbar);
        defaultFooterTitle = (TextView) view.findViewById(R.id.default_footer_title);
        defaultFooterTitle.setVisibility(View.GONE);
        defaultFooterProgressbar.setVisibility(View.GONE);
        mylistview.addFooterView(view);
    }

    @Override
    protected void initData() {
        super.initData();
        id = getIntent().getStringExtra("id");
        getEvaluateData();


    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mylistview.setOnScrollListener(this);
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();

        evaluateResult = gson.fromJson(response, ClearEvaluateResult.class);
        MyApplication.getInstance().setSpValue(evaluateResult.getSafeobj());
        if (evaluateResult.getComments() != null && evaluateResult.getComments().size() > 0) {

            if (pageNum == 1) {
                commentsList.clear();
            }
            try {
                JSONObject jsonObject = new JSONObject(response);
                user = jsonObject.optJSONObject("user");

            } catch (JSONException e) {
                e.printStackTrace();
            }
            commentsList.addAll(evaluateResult.getComments());
            evaluateAdapter = new ClearEvaluateAdapter(context, commentsList, user);
            mylistview.setAdapter(evaluateAdapter);
            nomore = false;
            noEvaluate.setVisibility(View.GONE);
        } else {
            if (pageNum==1){
                noEvaluate.setVisibility(View.VISIBLE);
            }
            pageNum--;
            nomore = true;

        }
        defaultFooterProgressbar.setVisibility(View.GONE);
    }

    /***
     * 获取评价列表信息
     */
    private void getEvaluateData() {
        String s = "&page=" + pageNum + "&goodsid=" + id;
        getServer(Url.CLEARWEED_EVALUATE, null, s, false, null, "get");
    }

    @OnClick(R.id.ivBack)
    public void onViewClicked() {
        onBackPressed();
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (islast && scrollState == SCROLL_STATE_IDLE) {
            pageNum++;
            if (!nomore) {
                getEvaluateData();
                defaultFooterProgressbar.setVisibility(View.VISIBLE);
            } else {
                defaultFooterTitle.setVisibility(View.VISIBLE);
                defaultFooterTitle.setText("已是全部数据了");
            }
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (firstVisibleItem + visibleItemCount == totalItemCount)
            islast = true;
        else
            islast = false;
    }

}
